<?php


namespace app\index\controller;


use app\index\controller\Base;
use app\index\model\Comments as CommentsModel;
use think\Db;

class Comments extends Base
{
    public function getinfo()
    {
        $res = new CommentsModel();
        $data = $this->request->param();
        $d = $res->with('commentsImg,user,commentList.commentsXh')
            ->where('c_id', '=', $data['c_id'])
            ->find();
        foreach ($d['comment_list'] as $list) {
            $getname = db('user')->field('name')
                ->where('u_id', '=', $list['send_u_id'])->find();
            $list['name'] = $getname['name'];
            foreach ($list['comments_xh'] as $xh) {
                $getxhname = db('user')->field('name')
                    ->where('u_id', '=', $xh['get_user'])->find();
                $sendxhname = db('user')->field('name')
                    ->where('u_id', '=', $xh['send_user'])->find();
                $xh['getname'] = $getname['name'];
                $xh['sendname'] = $sendxhname['name'];
            }
        }
        return $d;
    }
    public function getlist()
    {
        $res = new CommentsModel();
        $data = $this->request->param();
        $d = $res->with('commentsImg,user,commentList.commentsXh')
            ->where('comm_class', '=', $data['comm_class'])
            ->page($data['page'])
            ->limit(15)
            ->order('cTime desc')
            ->select();
        foreach ($d as $val) {
            foreach ($val['comment_list'] as $list) {
                $getname = db('user')->field('name')
                    ->where('u_id', '=', $list['send_u_id'])->find();
                foreach ($list['comments_xh'] as $xh) {
                    $getxhname = db('user')->field('name')
                        ->where('u_id', '=', $xh['get_user'])->find();
                    $sendxhname = db('user')->field('name')
                        ->where('u_id', '=', $xh['send_user'])->find();
                }
            }
        }
        return $d;
    }
    public function get()
    {
        $res = new CommentsModel();
        $data = $this->request->param();
        $d = $res->with('commentsImg,user')
            ->where($data['field'], $data['op'], $data['value'])
            ->where('foreign_id', $data['foreign_id'])
            ->where('comm_class', $data['comm_class'])
            ->page($data['page'])
            ->limit(10)
            ->order('cTime desc')
            ->select();
        return $d;
    }
    public function update()
    {
        $data = $this->request->param();
        $res = new CommentsModel();
        return $res->updateModel($data);
    }
    public function delete()
    {
        $data = $this->request->param();
        $res = new CommentsModel();
        return $res->deleteModel($data);
    }
    public function save()
    {
        $res = new CommentsModel();
        $data = $this->request->param();
        $res->saveModel($data);
        return $res->id;
    }
    public function countInc(){
        $res = new CommentsModel();
        $data = $this->request->param();
        $res->where([$data['field']=>$data['value']])->setInc($data['field1']);
    }
}
